﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Biz;
using System.Data.Entity;
//using System.Data;

namespace BusinessLayer.Dal
{
    class ProductDal
    {
        DatabaseContext db;
        public ProductDal( /* DatabaseContext db */ )
        {
            this.db = DatabaseContext.DB;
        }
        public Product GetProductById(int id)
        {
            return db.Products.Where(p => p.Id == id).First();
        }
        public List<Product> GetAllProduct()
        {
            return db.Products.Where(p=>p.IsDeleted == false).OrderByDescending(p=>p.Id).ToList();
        }

        public List<Product> GetAllFeatureProduct()
        {
            return db.Products.Where(p=>p.IsDeleted == false && p.IsFeatured==true).ToList();
        }
        public List<Product> GetByCategory(Category category)
        {
            return db.Products.Where(p => p.Category.Id==category.Id).ToList();
        }
        public static int Insert(Product product)
        {
            DatabaseContext.DB.Products.Add(product);
            return DatabaseContext.DB.SaveChanges();
        }

        public int Update(Product product)
        {
            db.Entry(product).State = EntityState.Modified;
            return db.SaveChanges();
        }

        public static void Delete(Product product)
        {
            var rs = DatabaseContext.DB.Products.Where(p => p.Id == product.Id).First() ;
            rs.IsDeleted = true;
            DatabaseContext.DB.SaveChanges();
        }

        public List<Product> Search(string name, int from, int to,int categoryId,int manufacturerId)
        {
            List<Product> products = db.Products.ToList();
            if (!String.IsNullOrEmpty(name))
            {
                products = db.Products.Where(p => p.Name.ToLower().Contains(name.ToLower())).ToList();
            }
            if (to < 0)
            {
                to = MaxPrice();
            }
            products = products.Where(p => p.CurrentPrice >= from && p.CurrentPrice <= to).ToList();
            if (categoryId > 0)
            {
                products = products.Where(p => p.Category.Id == categoryId).ToList();
            }
            if (manufacturerId > 0)
            {
                products = products.Where(p => p.Manufacturer.Id == manufacturerId).ToList();
            }
            return products;
        }

        public int MaxPrice()
        {
            return db.Products.Select(p => p.CurrentPrice).Max();
        }

        public static void Update(Product op, Product np)
        {
            var rs = DatabaseContext.DB.Products.Where(p => p.Id == op.Id).First();
            rs.Category = np.Category;
            rs.CurrentPrice = np.CurrentPrice;
            rs.Description = np.Description;
            rs.IsFeatured = np.IsFeatured;
            rs.Manufacturer = rs.Manufacturer;
            rs.Name = np.Name;
            rs.ShortDescription = np.ShortDescription;

            DatabaseContext.DB.SaveChanges();
        }
    }
}
